<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="父辈id，我是他的下线" prop="parentid">
      <el-input v-model="dataForm.parentid" placeholder="父辈id，我是他的下线"></el-input>
    </el-form-item>
    <el-form-item label="" prop="username">
      <el-input v-model="dataForm.username" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="realname">
      <el-input v-model="dataForm.realname" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="password">
      <el-input v-model="dataForm.password" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="email">
      <el-input v-model="dataForm.email" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="openid">
      <el-input v-model="dataForm.openid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="addrdefault">
      <el-input v-model="dataForm.addrdefault" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="sex">
      <el-input v-model="dataForm.sex" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="0:系统用户，1：商户服务员，2：普通用户，" prop="flag">
      <el-input v-model="dataForm.flag" placeholder="0:系统用户，1：商户服务员，2：普通用户，"></el-input>
    </el-form-item>
    <el-form-item label="积分" prop="soctt">
      <el-input v-model="dataForm.soctt" placeholder="积分"></el-input>
    </el-form-item>
    <el-form-item label="头像url" prop="imghead">
      <el-input v-model="dataForm.imghead" placeholder="头像url"></el-input>
    </el-form-item>
    <el-form-item label="用户等级" prop="level">
      <el-input v-model="dataForm.level" placeholder="用户等级"></el-input>
    </el-form-item>
    <el-form-item label="用户总余额," prop="moneytotal">
      <el-input v-model="dataForm.moneytotal" placeholder="用户总余额,"></el-input>
    </el-form-item>
    <el-form-item label="可用余额" prop="moneycanuse">
      <el-input v-model="dataForm.moneycanuse" placeholder="可用余额"></el-input>
    </el-form-item>
    <el-form-item label="冻结金额" prop="moneyfrozen">
      <el-input v-model="dataForm.moneyfrozen" placeholder="冻结金额"></el-input>
    </el-form-item>
    <el-form-item label="积分佣金额" prop="moneysoctt">
      <el-input v-model="dataForm.moneysoctt" placeholder="积分佣金额"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="wrtime">
      <el-input v-model="dataForm.wrtime" placeholder="注册时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userid: 0,
          parentid: '',
          username: '',
          realname: '',
          password: '',
          email: '',
          mobile: '',
          openid: '',
          addrdefault: '',
          sex: '',
          flag: '',
          soctt: '',
          imghead: '',
          level: '',
          moneytotal: '',
          moneycanuse: '',
          moneyfrozen: '',
          moneysoctt: '',
          wrtime: ''
        },
        dataRule: {
          parentid: [
            { required: true, message: '父辈id，我是他的下线不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          realname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          openid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          addrdefault: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          flag: [
            { required: true, message: '0:系统用户，1：商户服务员，2：普通用户，不能为空', trigger: 'blur' }
          ],
          soctt: [
            { required: true, message: '积分不能为空', trigger: 'blur' }
          ],
          imghead: [
            { required: true, message: '头像url不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '用户等级不能为空', trigger: 'blur' }
          ],
          moneytotal: [
            { required: true, message: '用户总余额,不能为空', trigger: 'blur' }
          ],
          moneycanuse: [
            { required: true, message: '可用余额不能为空', trigger: 'blur' }
          ],
          moneyfrozen: [
            { required: true, message: '冻结金额不能为空', trigger: 'blur' }
          ],
          moneysoctt: [
            { required: true, message: '积分佣金额不能为空', trigger: 'blur' }
          ],
          wrtime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userid) {
            this.$http({
              url: this.$http.adornUrl(`/portal/user/info/${this.dataForm.userid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentid = data.user.parentid
                this.dataForm.username = data.user.username
                this.dataForm.realname = data.user.realname
                this.dataForm.password = data.user.password
                this.dataForm.email = data.user.email
                this.dataForm.mobile = data.user.mobile
                this.dataForm.openid = data.user.openid
                this.dataForm.addrdefault = data.user.addrdefault
                this.dataForm.sex = data.user.sex
                this.dataForm.flag = data.user.flag
                this.dataForm.soctt = data.user.soctt
                this.dataForm.imghead = data.user.imghead
                this.dataForm.level = data.user.level
                this.dataForm.moneytotal = data.user.moneytotal
                this.dataForm.moneycanuse = data.user.moneycanuse
                this.dataForm.moneyfrozen = data.user.moneyfrozen
                this.dataForm.moneysoctt = data.user.moneysoctt
                this.dataForm.wrtime = data.user.wrtime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/portal/user/${!this.dataForm.userid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userid': this.dataForm.userid || undefined,
                'parentid': this.dataForm.parentid,
                'username': this.dataForm.username,
                'realname': this.dataForm.realname,
                'password': this.dataForm.password,
                'email': this.dataForm.email,
                'mobile': this.dataForm.mobile,
                'openid': this.dataForm.openid,
                'addrdefault': this.dataForm.addrdefault,
                'sex': this.dataForm.sex,
                'flag': this.dataForm.flag,
                'soctt': this.dataForm.soctt,
                'imghead': this.dataForm.imghead,
                'level': this.dataForm.level,
                'moneytotal': this.dataForm.moneytotal,
                'moneycanuse': this.dataForm.moneycanuse,
                'moneyfrozen': this.dataForm.moneyfrozen,
                'moneysoctt': this.dataForm.moneysoctt,
                'wrtime': this.dataForm.wrtime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
